package com.one.efaimaly.order.ui;

import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.one.common.common.order.AddMaterialItem;
import com.one.common.common.order.MaterialStateItem;
import com.one.common.common.order.binder.MaterialInfoBinder;
import com.one.common.common.order.binder.MaterialStateBinder;
import com.one.common.config.OrderState;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.utils.MyToast;
import com.one.common.utils.StringUtils;
import com.one.common.utils.Toaster;
import com.one.common.utils.map.LatLng;
import com.one.common.utils.map.MapUtils;
import com.one.common.view.pagestate.listpage.BaseListActivity;
import com.one.common.view.widget.MyTitleBar;
import com.one.efaimaly.R;
import com.one.efaimaly.order.model.extra.UploadImgExtra;
import com.one.efaimaly.order.model.item.OrderDetailItem;
import com.one.efaimaly.order.presenter.OrderPresenter;
import com.one.efaimaly.order.presenter.OrderStateHandle;
import com.one.efaimaly.order.ui.binder.OrderDetailBinder;
import com.one.efaimaly.order.view.OrderDetailView;
import com.one.efaimaly.utils.LocationUtils;
import com.tencent.map.geolocation.TencentLocation;

import static com.one.common.config.OrderState.PAY_END;
import static com.one.common.config.OrderState.WAIT_PAY;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/8/8 下午10:28
 * description ：订单详情
 **/
@Route(path = RouterPath.ORDER_DETAIL)
public class OrderDetailActivity extends BaseListActivity<OrderPresenter> implements OrderDetailView, LocationUtils.OnLocationListener {
    private TextView btn1, btn2, btn3;
    private OrderDetailItem orderDetailItem;
    private LatLng meLatlng;
    private String starName;

    @Override
    protected void initPresenter() {
        mPresenter = new OrderPresenter(this, this);
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        getMyTitleBar()
                .setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText("订单详情");
    }

    @Override
    public void registerDate() {
        register(OrderDetailItem.class, new OrderDetailBinder());
        register(MaterialStateItem.class, new MaterialStateBinder());
        register(AddMaterialItem.class, new MaterialInfoBinder());
    }

    @Override
    public void loadData() {
        mPresenter.getOrderDetail();
        LocationUtils.location(this, this);
    }

    @Override
    public void initView() {
        super.initView();
        hindLoadMore();
        hindRefresh();
        addBottomView(R.layout.layout_order_bottom);
        btn1 = successView.findViewById(R.id.tv_btn_cancel);
        btn2 = successView.findViewById(R.id.tv_btn_navigation);
        btn3 = successView.findViewById(R.id.tv_btn_confirm);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
    }

    @Override
    public void setOrderDetail(OrderDetailItem item) {
        if (item != null) {
            orderDetailItem = item;
            OrderStateHandle.setStateView(item, btn1, btn2, btn3);
            if(item.getOrder_state().equals(OrderState.WAIT_CONFIRM)){
                // 滚动到底部
                getListView().scrollToPosition(getAdapter().getItemCount()-1);
            }
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        doRefresh();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_btn_cancel:
                if(orderDetailItem.getOrder_state().equals(PAY_END)){
                    if(StringUtils.isBlank(orderDetailItem.getOrder_maintail_imgs1()) &&
                            StringUtils.isBlank(orderDetailItem.getOrder_maintail_imgs2())){
                        // 上传图片
                        RouterManager.getInstance().go(RouterPath.UPLOAD_IMG, new UploadImgExtra(orderDetailItem.getOrder_id()+""));
                    } else {
                      // 查看图片
                        RouterManager.getInstance().go(RouterPath.UPLOAD_IMG,
                                new UploadImgExtra(orderDetailItem.getOrder_maintail_imgs1(),
                                orderDetailItem.getOrder_maintail_imgs2(),orderDetailItem.getOrder_id()+""));
                    }
                } else { // 取消订单
                    mPresenter.cancel();
                }
                break;
            case R.id.tv_btn_navigation:
                if (orderDetailItem != null && orderDetailItem.getMaintailOrderAddressBean() != null) {
                    MapUtils.selectMap(this, meLatlng, starName,
                            orderDetailItem.getMaintailOrderAddressBean().getLatLng(),
                            orderDetailItem.getMaintailOrderAddressBean().getAddress());
                }
                break;
            case R.id.tv_btn_confirm:
                // 这里要传入 已选择的材料，连带id
                RouterManager.getInstance().go(RouterPath.ADD_MATERIAL, mPresenter.getListExtra());
                break;
            default:
                break;
        }
    }

    @Override
    public void onLocated(TencentLocation aMapLocation) {
        if (aMapLocation != null) {
            starName = aMapLocation.getAddress();
            meLatlng = new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude());
        }
    }

    @Override
    public void onLocationPermissionFail() {
        Toaster.showLongToast(StringUtils.format(R.string.permission_fail_local), MyToast.FAIL);
    }
}
